package cn.com.dashihui.web.controller;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.aop.Duang;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

import cn.com.dashihui.kit.DatetimeKit;
import cn.com.dashihui.kit.ExcelExportUtil.Pair;
import cn.com.dashihui.web.base.BaseController;
import cn.com.dashihui.web.dao.Store;
import cn.com.dashihui.web.service.StoreService;
import cn.com.dashihui.web.service.StoreTongService;

public class StoreTongController extends BaseController{
	private StoreTongService storerTongService = Duang.duang(StoreTongService.class);
	private StoreService storeService = new StoreService();
	/**
	 * 首页
	 */
	public void index(){
		Record record=storerTongService.countStore();
		setAttr("tolnumber", record.get("tolnumber"));
		setAttr("sjshnumber", record.get("sjshnumber"));
		setAttr("sjnumber", record.get("sjnumber"));
		setAttr("xjnumber", record.get("xjnumber"));
		List<Record> list=storeService.findAllSeller();
    	setAttr("list", list);
    	List<Record> listStore=storeService.findAllStore();
		setAttr("listStore", listStore);
		render("index.jsp");
	}
	/**
	 * 分页查询
	 */
	 public void page(){
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			String storeIds = getPara("storeIds");
			String sellerId = getPara("sellerId");
			String sStoreId = getPara("sStoreId");
			String sBeginDate = getPara("sBeginDate");
			String sEndDate = getPara("sEndDate");
			int state = getParaToInt("state",0);
			renderResult(0,storerTongService.findByPage(pageNum, pageSize,storeIds,sellerId, sStoreId,sBeginDate,sEndDate,state));
		}
	 /**
		 * 分页排序查询
		 */
		 public void page1(){
			    String index=getPara("index");
				int pageNum = getParaToInt(0, 1);
				int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
				String storeIds = getPara("storeIds");
				String sellerId = getPara("sellerId");
				String sStoreId = getPara("sStoreId");
				String sBeginDate = getPara("sBeginDate");
				String sEndDate = getPara("sEndDate");
				int state = getParaToInt("state",0);
				renderResult(0,storerTongService.findByPage1(pageNum, pageSize,index,storeIds,sellerId, sStoreId,sBeginDate,sEndDate,state));
			}
	 public void xspage(){
			int pageNum = getParaToInt(0, 1);
			int pageSize = getParaToInt("pageSize", PropKit.getInt("constants.pageSize"));
			String storeIds = getPara("storeIds");
			String sellerId = getPara("sellerId");
			String sStoreId = getPara("sStoreId");
			String sBeginDate = getPara("sBeginDate");
			String sEndDate = getPara("sEndDate");
			int date =getParaToInt("date", 0);
			int state = getParaToInt("state",0);
			renderResult(0,storerTongService.findXSByPage(pageNum, pageSize, storeIds,sellerId, sStoreId,sBeginDate,sEndDate,date,state));
		}
	 /**
	  * 编辑分公司
	  */
		public void toEdit(){
			String storeid = getPara("storeid");
			if(!StrKit.isBlank(storeid)){
				setAttr("object", storerTongService.findById(storeid));
			}
			render("edit.jsp");
		}
		public void edit(){
			String storeid=getPara("storeid");
			int qstate=getParaToInt("qstate",0);
			if(!StrKit.isBlank(storeid)&&qstate!=0){
				Store store=storerTongService.findByStoreId(storeid);
				store.set("qstate", qstate);
				store.set("checktime",  DatetimeKit.getFormatDate("yyyy-MM-dd HH:mm:ss"));
				if(storerTongService.edit(store)){
					renderSuccess(storerTongService.findPageByStoreid(storeid));
					return;
				}else{
					renderResult(1);
					return;
				}
			}else{
				renderResult(1);
				return;
			}
		}
		/**
		 * excel导出
		 */
		public void doExcel(){
			List<Pair> titles = new ArrayList<Pair>();
		    titles.add(new Pair("storeid", "分公司ID"));
		    titles.add(new Pair("title", "分公司名称"));
		    titles.add(new Pair("scope", "主营范围"));
		    titles.add(new Pair("sellerName", "所属公司"));
		    titles.add(new Pair("starttime", "开店时间 "));
		    titles.add(new Pair("aname", "分公司联系人"));
		    titles.add(new Pair("aphone", "联系方式"));
		    titles.add(new Pair("countGoods", "产品数量"));
		    titles.add(new Pair("countNum", "订单量"));
		    titles.add(new Pair("countMoney", "销售额"));
		    titles.add(new Pair("qstate", "分公司状态"));
		    String storeIds = getPara("storeIds");
			String sellerId = getPara("sellerId");
			String sStoreId = getPara("sStoreId");
			String sBeginDate = getPara("sBeginDate");
			String sEndDate = getPara("sEndDate");
			int date =getParaToInt("date", 0);
			int state = getParaToInt("state",0);
			List<Record> list=storerTongService.findExcelByPage( storeIds,sellerId, sStoreId,sBeginDate,sEndDate,date,state);
			List<Record> listz=new ArrayList<Record>();
			for (int i = 0; i < list.size(); i++) {
				Record record=new Record();
					record.set("storeid",list.get(i).get("storeid"));
					record.set("title", list.get(i).get("title"));
					if(list.get(i).get("scope").equals(1)){
						record.set("scope", "常规");
					}else if(list.get(i).get("scope").equals(2)){
						record.set("scope", "团购");
					}else if(list.get(i).get("scope").equals(3)){
						record.set("scope", "常规+团购");
					}
					record.set("sellerName", list.get(i).get("sellerName"));
					record.set("starttime", list.get(i).get("starttime"));
					record.set("aname", list.get(i).get("aname"));
					record.set("aphone", list.get(i).get("aphone"));
					record.set("countGoods", list.get(i).get("countGoods"));
					record.set("countNum", list.get(i).get("countNum"));
					record.set("countMoney", list.get(i).get("countMoney"));
					if(list.get(i).get("qstate").equals(1)){
						if (list.get(i).get("state").equals(1)) {
							record.set("qstate", "上线审核中");
						}else if (list.get(i).get("state").equals(2)) {
							record.set("qstate", "上线");
						}else if (list.get(i).get("state").equals(3)) {
							record.set("qstate", "下线");
						}else if (list.get(i).get("state").equals(4)) {
							record.set("qstate", "驳回");
						}
						
					}else if(list.get(i).get("qstate").equals(2)){
						record.set("qstate", "禁货");
					}else if (list.get(i).get("qstate").equals(3)){
						record.set("qstate", "降权");
					}
				listz.add(record);
	 		}
			// 导出
			storerTongService.export(getResponse(), getRequest(),titles, listz);		
			renderNull();
		}
}
